package cn.cdeden.knowledge.service.impl;

import cn.cdeden.common.core.utils.MapstructUtils;
import cn.cdeden.common.core.utils.page.TableDataInfo;
import cn.cdeden.common.mybatis.validator.QueryHelpMybatisPlus;
import cn.cdeden.common.satoken.utils.LoginHelper;
import cn.cdeden.knowledge.domain.TeamType;
import cn.cdeden.knowledge.domain.dto.TeamTypeDto;
import cn.cdeden.knowledge.domain.dto.TeamTypeQueryParam;
import cn.cdeden.knowledge.mapper.TeamTypeMapper;
import cn.cdeden.knowledge.service.TeamTypeService;
import cn.cdeden.knowledge.utils.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* @author Chen
* @date 2024-04-19
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = TeamTypeService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class TeamTypeServiceImpl extends ServiceImpl<TeamTypeMapper, TeamType> implements TeamTypeService {

    // private final RedisUtils redisUtils;
    private final TeamTypeMapper teamTypeMapper;

    @Override
    public TableDataInfo<TeamTypeDto> queryAll(TeamTypeQueryParam query, Pageable pageable) {
        IPage<TeamType> queryPage = PageUtil.toMybatisPage(pageable);
        QueryWrapper<TeamType> predicate = QueryHelpMybatisPlus.getPredicate(query);
        predicate.eq("create_id", LoginHelper.getUserId());
        IPage<TeamType> page = teamTypeMapper.selectPage(queryPage,predicate );
        TableDataInfo<TeamType> build = TableDataInfo.build(page);
        TableDataInfo<TeamTypeDto> teamTypeDtoTableDataInfo = MapstructUtils.convertTableDataInfo(build, TeamTypeDto.class);
//        PageInfo<TeamTypeDto> teamTypeDtoPageInfo = ConvertUtil.convertPage(page, TeamTypeDto.class, pageable);
        for (TeamTypeDto teamTypeDto : teamTypeDtoTableDataInfo.getRows()) {
            teamTypeDto.setCreateName(teamTypeDto.getCreateId());
        }
        return teamTypeDtoTableDataInfo;
    }

    @Override
    public List<TeamTypeDto> queryAll(TeamTypeQueryParam query){
        return MapstructUtils.convert(teamTypeMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), TeamTypeDto.class);
    }

    @Override
    public Map<Integer, Map<String, Object>> selectTypeCount() {
        return teamTypeMapper.selectTypeCount();
    }

    @Override
    public TeamType getById(Integer id) {
        return teamTypeMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public TeamTypeDto findById(Integer id) {
        return MapstructUtils.convert(getById(id), TeamTypeDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insert(TeamTypeDto resources) {
        TeamType entity = MapstructUtils.convert(resources, TeamType.class);
        return this.save(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateById(TeamTypeDto resources){
        TeamType entity = MapstructUtils.convert(resources, TeamType.class);
        boolean ret = this.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteByIds(Set<Integer> ids){
        // delCaches(ids);
        return this.removeByIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Integer id){
        Set<Integer> set = new HashSet<>(1);
        set.add(id);
        return this.deleteByIds(set);
    }

    /*
    private void delCaches(Integer id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Integer> ids) {
        for (Integer id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<TeamTypeDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (TeamTypeDto teamType : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("分类", teamType.getName());
              map.put("创建编号", teamType.getCreateId());
              map.put("创建日期", teamType.getCreateTime());
              map.put("更新编号", teamType.getUpdateId());
              map.put("更新时间", teamType.getUpdateTime());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
